﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BrainTechLLC;
using System.Windows.Threading;
using System.Windows;
using BrainTechLLC.ThreadSafeObjects;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization;

namespace BrainTechLLC.WPF
{
    [Serializable]
    [DataContract]
    public class ClassAndPropertyChangeUI : ClassAndPropertyChange
    {
        [DataMember]
        public bool UIThread;

        public override bool EqualsOther(ClassAndPropertyChange o)
        {
            ClassAndPropertyChangeUI other = o as ClassAndPropertyChangeUI;
            
            if (other == null)
                return base.EqualsOther(o);

            return (PropertyName == other.PropertyName) && (UIThread = other.UIThread) && (ClassObject == other.ClassObject);
        }

        public ClassAndPropertyChangeUI(BaseNotifyPropertyChanged classObject, string propertyName, bool uiThread)
            : base(classObject, propertyName)
        {
            UIThread = uiThread;
        }

        public override void RaisePropertyChanged()
        {
#if SILVERLIGHT
            if (!UIThread)
            {
                ClassObject.RaisePropertyChanged(PropertyName);
            }
            // TODO: 
            //else
            //{
            //    ClassObject.RaisePropertyChangedUIThread(PropertyName);
            //}
#else
            if (ClassObject._propertyChanged != null && ClassObject._propertyChanged.eventEntries.Count > 0)
            {
                if (!UIThread)
                {
                    ClassObject.RaisePropertyChanged(PropertyName);
                }
                else
                {
                    BaseNotifyPropertyChangedUI notifyUI = ClassObject as BaseNotifyPropertyChangedUI;
                    if (notifyUI != null)
                    {
                        notifyUI.RaisePropertyChangedUIThread(PropertyName);
                    }
                    else
                    {
                        ClassObject.RaisePropertyChanged(PropertyName);
                    }
                }
            }
#endif
        }
    }
}
